package com.bjx.common.core.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.bjx.common.core.domain.bo.FileInfoBO;

import java.util.Date;

/**
 *  文件工具类
 */
public class FileUtils {


    /**
     * 文件名称重命名
     * @param originalFileName
     * @return: com.bjx.common.core.domain.bo.FileInfoBO
     * @Author: liushuai
     * @Date: 2023/5/15 15:37
     */
    public static FileInfoBO renameFileName(String originalFileName){
        if(StrUtil.isEmpty(originalFileName)){
            throw new IllegalStateException("文件名称不能为空");
        }
        Date currentDate = new Date();
        //文件类型
        String fileType = cn.hutool.core.io.FileUtil.getSuffix(originalFileName);
        //重命名之后的文件名称
        String saveFileName = StrUtil.format("{}_{}.{}",cn.hutool.core.io.FileUtil.mainName(originalFileName),
                IdUtil.getSnowflake(),fileType
                );
        //文件保存的url
        String saveUrl =  StrUtil.format("{}/{}/{}/{}",
                DateUtil.year(currentDate),
                DateUtil.month(currentDate) + 1,
                DateUtil.dayOfMonth(currentDate),
                saveFileName
                );
        return FileInfoBO.builder()
                .saveFileName(saveFileName)
                .fileType(fileType)
                .saveUrl(saveUrl)
                .build();
    }



}
